<?php

namespace App\Jobs;

use App\Model\User;
use App\Repository\LostRepository;
use App\Repository\UserRepository;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class LostNotify implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    protected $lost;
    protected $repo;
    /**
     * @var UserRepository
     */
    protected $userRepository;

    protected $user;

    /**
     * Create a new job instance.
     * @param $lost
     */
    public function __construct($lost)
    {
        $this->lost = $lost;
    }

    /**
     * Execute the job.
     * @param UserRepository $user
     * @param LostRepository $lost
     */
    public function handle(UserRepository $user, LostRepository $lost)
    {
        $this->userRepository = $user;

        if($this->lost->number){

            //$this->user 为用户信息数组
            $this->user = \LostService::updateUserOfLost($this->lost->id);

            if($this->user){

                $owner = $user->getUserByNumber($this->lost->number);

                $this->lost = $lost->find($this->lost->id);

                if($owner){
                    $this->notifyOwner($owner->open_id);
                }else{
                    $this->notifyClassmates();
                }

            }
        }

    }

    /**
     * 通知失主本人
     * @param $openId
     */
    private function notifyOwner($openId)
    {
        $this->notify($openId, true);
    }

    /**
     * 通知同班同学
     */
    private function notifyClassmates()
    {
            $receivers = $this->userRepository->getClassmatesByClassName($this->lost->class ?? null);

            if($receivers->count()){
                $this->notifyGroup($receivers, '你同班同学');
            }
            else{
                $this->notifySameGrade();
            }

    }

    /**
     * 通知隔壁班同学
     */
    private function notifySameGrade()
    {
        $users = $this->userRepository->getSameGradeMajor($this->user['grade'], $this->user['major']);

        if($users){
            $this->notifyGroup($users, '你隔壁班同学');
        }
    }

    /**
     * 通知群体
     *
     * @param $receivers
     * @param $msg
     * @return bool
     */
    private function notifyGroup($receivers, $msg)
    {
        if($receivers){
            $receivers = $receivers->shuffle()->take(5)->pluck('open_id');
            $this->notify($receivers->all(), false, $msg);
            return true;
        }else{
            return false;
        }
    }

    /**
     * 发送通知
     *
     * @param $openId
     * @param bool $isOwner
     * @param string $who
     */
    private function notify($openId, $isOwner = false, $who = '你')
    {
        $publisher = $this->userRepository->getUserByOpenId($this->lost->publisher);

        $data = [
            'first' => "您好，{$who}的物品已被拾获",
            'keyword1' => $this->getPublisherTitle($publisher),
            'keyword2' => $this->lost->description,
            'keyword3' => $this->lost->class . ' ' . $this->lost->name,
            'remark' => $isOwner ? '请尽快登录系统联系拾获者' : '请麻烦联系失主通知其登录平台查看详细信息',
        ];

        dispatch(new SendTemplate($data, env('APP_URL') . 'lost', $openId, 'lost_publish'));
    }

    /**
     * 获取发布人信息
     *
     * @param User $publisher
     * @return string
     */
    private function getPublisherTitle(User $publisher)
    {
        return substr($publisher->name, 0, 3) . '同学';
    }

}
